package com.example.mediaandroidjava;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.common.MediaItem;
import androidx.media3.common.PlaybackException;
import androidx.media3.common.Player;
import androidx.media3.datasource.HttpDataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.util.EventLogger;
import androidx.media3.ui.PlayerView;

import android.nfc.Tag;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;

///Hello ExoPlayer & Player Event
public class MainActivity extends AppCompatActivity {

    private PlayerView playerView;
    private ExoPlayer exoPlayer;
    private static String TAG = "MainActivityTag";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //配置player
        playerView = findViewById(R.id.player_view);
        exoPlayer = new ExoPlayer.Builder(this).build();
        playerView.setPlayer(exoPlayer);
        MediaItem mediaItem = MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
        //MediaItem mediaItem = MediaItem.fromUri("error");
        exoPlayer.setMediaItem(mediaItem);
        exoPlayer.prepare();
        exoPlayer.play();

        ///监听播放器的状态
        exoPlayer.addListener(new Player.Listener() {
            @Override
            public void onIsPlayingChanged(boolean isPlaying) {
                Player.Listener.super.onIsPlayingChanged(isPlaying);
                Log.i(TAG, "isPlaying:" + isPlaying);
            }

            @Override
            public void onPlayerError(@NonNull PlaybackException error) {
                Player.Listener.super.onPlayerError(error);
                @Nullable Throwable cause = error.getCause();
                if (cause instanceof HttpDataSource.HttpDataSourceException) {
                    // An HTTP error occurred.
                    HttpDataSource.HttpDataSourceException httpError = (HttpDataSource.HttpDataSourceException) cause;
                    if (httpError instanceof HttpDataSource.InvalidResponseCodeException) {
                        Log.i(TAG, "HttpDataSource.InvalidResponseCodeException");
                    } else {
                        Log.i(TAG, "not HttpDataSource.InvalidResponseCodeException");
                    }
                }
            }

            @Override
            public void onEvents(Player player, Player.Events events) {
                Player.Listener.super.onEvents(player, events);
                if (events.contains(Player.EVENT_PLAYBACK_STATE_CHANGED) || events.contains(Player.EVENT_PLAY_WHEN_READY_CHANGED)) {
//                    uiModule.updateUi(player);
                    Log.i(TAG, "状态改变");
                    for (int i = 0; i < events.size(); i++) {
                        Log.i(TAG, String.valueOf(events.get(i)));
                    }

                }
            }
        });

        ///日志
        ///adb logcat EventLogger:* ExoPlayerImpl:* *:s
        exoPlayer.addAnalyticsListener(new EventLogger());
        exoPlayer.createMessage((messageType, payload) -> {
                    // Do something at the specified playback position.
                    Log.i(TAG, "视频播放至五秒时触发");
                }).setLooper(Looper.getMainLooper()).setPosition(/* mediaItemIndex= */ 0, /* positionMs= */ 5_000)
//                .setPayload(customPayloadData)
                .setDeleteAfterDelivery(false).send();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放资源
        exoPlayer.release();
    }
}